package com.glory.good_dark.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.TextView;

import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.good_dark.R;
import com.glory.good_dark.activity.ReplaceMaterielActivity;
import com.glory.good_dark.entity.LotInfoEntity;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * FileName: BatchWorkBeginListAdapter
 * Author: XJH
 * Date: 2020/5/31 17:47
 * Description:
 * History:
 */
public class BatchWorkBeginListAdapter extends BaseExpandableListAdapter {

    private List<LotInfoEntity> mGroupList;
    private Activity mContext;
    private FreeUI_GeneralFragmentDialog scanDialog;
    private FragmentManager mFragmentManger;

    public BatchWorkBeginListAdapter(Activity context,FragmentManager fragmentManager) {
        this.mContext = context;
        mGroupList = new ArrayList<>();
        this.mFragmentManger = fragmentManager;
    }

    public void addData(LotInfoEntity data){
        this.mGroupList.add(data);
        notifyDataSetChanged();
    }

    public void setData(List<LotInfoEntity> data){
        this.mGroupList.clear();
        this.mGroupList.addAll(data);
        notifyDataSetChanged();
    }

    public List<LotInfoEntity> getData(){
        return mGroupList;
    }


    @Override
    public int getGroupCount() {
        return mGroupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public LotInfoEntity getGroup(int groupPosition) {
        return mGroupList.get(groupPosition);
    }

    @Override
    public LotInfoEntity getChild(int groupPosition, int childPosition) {
        return mGroupList.get(groupPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition * 100;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return groupPosition*100+childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_batchusemateriel_group,null);
        TextView tvLot = view.findViewById(R.id.tvLot);
        tvLot.setText(getGroup(groupPosition).getLOT_ID());
        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolder vh;
        if (convertView == null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_batchusemateriel_child
                    ,null);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        }else {
            vh = (ViewHolder) convertView.getTag();
        }


        vh.tvNumber.setText(String.valueOf(getGroup(groupPosition).getINPUT_QTY()));
        vh.tvProcess.setText(getGroup(groupPosition).getPROCESS());
        vh.tvProduct.setText(getGroup(groupPosition).getPART_NAME());
        vh.btnAdjustable.setOnClickListener(v -> {
            vh.rvMateriel.setVisibility(vh.rvMateriel.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
            vh.btnAdjustable.setText(vh.rvMateriel.getVisibility() == View.VISIBLE ? "收起" : "展开");
        });

        vh.btnReplaceMateriel.setOnClickListener(v -> FreeApi_Utils.skipWithExtraIntent("header", mContext, ReplaceMaterielActivity.class));
        vh.rvMateriel.setLayoutManager(new LinearLayoutManager(mContext));
        vh.rvMateriel.setItemAnimator(new DefaultItemAnimator());
        vh.rvMateriel.addItemDecoration(new DividerItemDecoration(mContext, 1));
        MaterielListAdapter materielListAdapter = new MaterielListAdapter(mGroupList.get(groupPosition).getMES_MAT_LIST(), false);
        materielListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        materielListAdapter.isFirstOnly(true);
        vh.rvMateriel.setAdapter(materielListAdapter);
        vh.tvProcess.setOnClickListener(v -> {
            scanDialog = FreeApi_DialogUtils.getScanDialog(mContext, "请输入或扫描运输筐号", s -> {
                mGroupList.get(groupPosition).setPROCESS(s);
                vh.tvProcess.setText(s);
                scanDialog.dismiss();
            });

           scanDialog.show(mFragmentManger,"WEB_URL_DIALOG");
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    public class ViewHolder{
        TextView tvProcess;
        TextView tvProduct;
        TextView tvNumber;
        Button btnAdjustable;
        Button btnReplaceMateriel;
        RecyclerView rvMateriel;

        public ViewHolder(View convertView) {
            tvProcess = convertView.findViewById(R.id.process_Txt);
            tvProduct = convertView.findViewById(R.id.product_Txt);
            tvNumber = convertView.findViewById(R.id.number_Txt);
            btnAdjustable = convertView.findViewById(R.id.adjustable_But);
            btnReplaceMateriel = convertView.findViewById(R.id.replaceMateriel_But);
            rvMateriel = convertView.findViewById(R.id.materiel_Rv);
        }
    }
}
